package com.qs.eggs.account;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.R;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.utils.CityDialogHelper;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

public class RegisterStepTwoActivity extends BaseActivity implements View.OnClickListener{
    private EditText mPwd,mOtherPeople;
    private TextView tvProvince,tvCity,tvCounty,tvCommunity,tvHeader;
    private LinearLayout mProvince,mCity,mCounty,mCommunity,mHeader;
    private String mCode,mPhone;

    private TextView mSubbmit;
    private TextView tvForIdP,tvForIdCity,tvForIdCounty,tvForIdCommunity,tvForIdHeader;
    private String ProvinceId="",CityId="",CountyId="",CommunityId="",HeaderId="";
    @Override
    protected int getContentView() {
        return R.layout.activity_register_step_two;
    }

    @Override
    protected void initWidget() {
        super.initWidget();

        mPhone=getIntent().getStringExtra("phone");
        mCode=getIntent().getStringExtra("code");

        mPwd=(EditText)findViewById(R.id.et_pwd);
        mOtherPeople=(EditText)findViewById(R.id.et_other);
        tvProvince=(TextView)findViewById(R.id.tv_province);
        tvCity=(TextView)findViewById(R.id.tv_city);
        tvCounty=(TextView)findViewById(R.id.tv_county);
        tvCommunity=(TextView)findViewById(R.id.tv_community);
        tvHeader=(TextView)findViewById(R.id.tv_header);
        mSubbmit=(TextView)findViewById(R.id.tv_subbmit);
        mProvince=(LinearLayout)findViewById(R.id.lly_province);
        mCity=(LinearLayout)findViewById(R.id.lly_city);
        mCounty=(LinearLayout)findViewById(R.id.lly_county);
        mCommunity=(LinearLayout)findViewById(R.id.lly_community);
        mHeader=(LinearLayout)findViewById(R.id.lly_header);

        tvForIdP=(TextView)findViewById(R.id.tv_forid_p);
        tvForIdCity=(TextView)findViewById(R.id.tv_forid_city);
        tvForIdCounty=(TextView)findViewById(R.id.tv_forid_county);
        tvForIdCommunity=(TextView)findViewById(R.id.tv_forid_ommunity);
        tvForIdHeader=(TextView)findViewById(R.id.tv_forid_header);

        mProvince.setOnClickListener(this);
        mCity.setOnClickListener(this);
        mCounty.setOnClickListener(this);
        mCommunity.setOnClickListener(this);
        mHeader.setOnClickListener(this);
        mSubbmit.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //加载省列表
            case R.id.lly_province:
                HttpApi.getProvince(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        try {
                            ArrayList<String> mProvincesList=new ArrayList<String>();
                            ArrayList<String> mProvincesListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mProvincesList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mProvincesListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            CityDialogHelper.getCity(RegisterStepTwoActivity.this,mProvincesList,mProvincesListId,tvProvince,tvForIdP);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                     }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });
                break;

            //加载市列表
            case R.id.lly_city:
                if(tvProvince.getText().toString().trim().length()==0){
                    Toast.makeText(RegisterStepTwoActivity.this,"请先选择省",Toast.LENGTH_SHORT).show();
                    return;
                }
                ProvinceId=tvForIdP.getText().toString();
                HttpApi.getCity(ProvinceId,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        try {
                            ArrayList<String> mCityList=new ArrayList<String>();
                            ArrayList<String> mCityListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mCityList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mCityListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            CityDialogHelper.getCity(RegisterStepTwoActivity.this,mCityList,mCityListId,tvCity,tvForIdCity);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });
                break;

            //加载县列表
            case R.id.lly_county:
                if(tvCity.getText().toString().trim().length()==0){
                    Toast.makeText(RegisterStepTwoActivity.this,"请先选择市",Toast.LENGTH_SHORT).show();
                    return;
                }
                CityId=tvForIdCity.getText().toString();
                HttpApi.getCounty(CityId,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        try {
                            ArrayList<String> mCountyList=new ArrayList<String>();
                            ArrayList<String> mCountyListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mCountyList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mCountyListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            CityDialogHelper.getCity(RegisterStepTwoActivity.this,mCountyList,mCountyListId,tvCounty,tvForIdCounty);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });
                break;

            //加载小区列表
            case R.id.lly_community:
                if(tvCounty.getText().toString().trim().length()==0){
                    Toast.makeText(RegisterStepTwoActivity.this,"请先选择县",Toast.LENGTH_SHORT).show();
                    return;
                }
                CountyId=tvForIdCounty.getText().toString();
                HttpApi.getCommunity(CountyId,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        try {
                            ArrayList<String> mCommunityList=new ArrayList<String>();
                            ArrayList<String> mCommunityListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mCommunityList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mCommunityListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            CityDialogHelper.getCity(RegisterStepTwoActivity.this,mCommunityList,mCommunityListId,tvCommunity,tvForIdCommunity);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });
                break;

            //加载负责人列表
            case R.id.lly_header:
                if(tvCommunity.getText().toString().trim().length()==0){
                    Toast.makeText(RegisterStepTwoActivity.this,"请先选择小区",Toast.LENGTH_SHORT).show();
                    return;
                }
                CommunityId=tvForIdCommunity.getText().toString();
                HttpApi.getHeader(CommunityId,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        try {
                            ArrayList<String> mHeaderList=new ArrayList<String>();
                            ArrayList<String> mHeaderListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mHeaderList.add(jsonArray.getJSONObject(i).getString("accountname"));
                                mHeaderListId.add(jsonArray.getJSONObject(i).getString("accountid"));
                            }

                            CityDialogHelper.getCity(RegisterStepTwoActivity.this,mHeaderList,mHeaderListId,tvHeader,tvForIdHeader);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });
                break;
            case R.id.tv_subbmit:
                upRegisterInfo();
                break;
        }
    }

    /**
     * 提交注册
     */

    private void upRegisterInfo(){
        String pwdS=mPwd.getText().toString().trim();
        if(pwdS.length()==0){
            Toast.makeText(RegisterStepTwoActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
            return;
        }
        HashMap<String,String> map=new HashMap<>();
        map.put("custname",mPhone);
        map.put("phone",mPhone);
        map.put("name",mPhone);
        map.put("accountname",mPhone);
        map.put("password",pwdS);
        map.put("isvalid","1");
        map.put("score","0");
        map.put("balance","0");
        map.put("provinceid",ProvinceId);
        map.put("cityid",CityId);
        map.put("areaid",CountyId);
        map.put("villageid",CommunityId);
        map.put("chargerid",HeaderId);
        map.put("chargername","");
        map.put("recommenderid","");
        map.put("recommendername","");

        HttpApi.upRegister(map, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {

                if(s.equals("\"true\"")){
                  Toast.makeText(RegisterStepTwoActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                    RegisterStepTwoActivity.this.finish();
                    Intent intent=new Intent(RegisterStepTwoActivity.this,LoginActivity.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(RegisterStepTwoActivity.this,"注册失败",Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
            }
        });
    }

}
